<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/22
 * Time: 下午2:01
 */

namespace App\Http\Model;

use http\Env\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class RoomUser extends Base
{
    private $redis_room_user_key = "ROOM:USER:"; //ROOM:USER:{room_id}
    private $redis_room_user_map_key = "ROOM:USER:MAP"; //ROOM:USER:MAP
    private $redis_join_allow = 'ROOM:USER:JOIN:ALLOW'; //认证和18禁
    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    public function removeUserFromRongyun($user_id)
    {
        $redisKey = 'MAP';
        Redis::HDEL($this->redis_room_user_key . $redisKey, $user_id);

        $roomIds = DB::table('room')->pluck("room_id");
        foreach ($roomIds as $roomId) {
            $redisVal = Redis::HEXISTS($this->redis_room_user_key . $roomId, $user_id);
            if ($redisVal) {
                Redis::HDEL($this->redis_room_user_key . $roomId, $user_id);
            }
        }
    }

    /**
     * 清除Redis 数据
     * @param $room_id
     */
    public function remove($room_id){
        $redis_user_key = $this->redis_room_user_key . $room_id;
        Redis::DEL($redis_user_key);
    }


    /**
     * 添加用户
     * @param $room_id
     * @param $user_id
     * @return bool
     */
    public function add_user($room_id, $user_id){
        $redis_user_key = $this->redis_room_user_key . $room_id;
        $ret = Redis::HSETNX($redis_user_key, $user_id, time());
        if ($ret){
            Redis::HSET($this->redis_room_user_map_key, $user_id, $room_id);
        }
        return $ret;
    }


    /**
     * 删除用户
     * @param $room_id
     * @param $user_id
     * @return mixed
     */
    public function del_user($room_id, $user_id){
        $redis_user_key = $this->redis_room_user_key . $room_id;
        $ret = Redis::HDEL($redis_user_key, $user_id);
        Redis::HDEL($this->redis_room_user_map_key, $user_id);
        return true;
    }


    /**
     * 获取房间用户ID
     * @param $room_id
     * @return array|null
     */
    public function get_usersids($room_id){
        $redis_user_key = $this->redis_room_user_key . $room_id;
        $redis_ret = Redis::HKEYS($redis_user_key);
        if(empty($redis_ret)){
            return null;
        }
        if(!is_array($redis_ret)){
            return [];
        }
        return $redis_ret;
    }

    public function exists($room_id, $user_id){
        $redis_user_key = $this->redis_room_user_key . $room_id;
        return Redis::HEXISTS($redis_user_key, $user_id);
    }


    public function get_users_count($room_id){
        $redis_user_key = $this->redis_room_user_key . $room_id;
        return Redis::HLEN($redis_user_key);
    }

    /**
     * 获取用户当前所在房间
     * @param $user_id
     */
    public function get_user_cur_room($user_id){
        $ret = Redis::HGET($this->redis_room_user_map_key, $user_id);
        if (empty($ret)){
            return null;
        }
        return intval($ret);
    }


    /**
     * 获取所有房间内用户
     * @return array
     */
    public function getRoomUserIdArr($roomId){
        $redis_user_key = $this->redis_room_user_key.$roomId;
        $ret = Redis::HKEYS($redis_user_key);
        if (empty($ret)){
            return [];
        }
        return $ret;
    }

    /**
     * 允许访问房间类型
     * @param $type 0.未认证\1.18岁以下
     */
    public function joinAllowType($type){
        if (!Redis::HEXISTS($this->redis_join_allow, $type)){ //未定义,所用分类都可以访问
            return -1;
        }
        $typeStr = Redis::HGET($this->redis_join_allow, $type);
        if (empty($typeStr)){
            return [];
        }
        $typeArr = explode(',' , $typeStr);
        return $typeArr;
    }



    /*
    * 允许访问房间类型
    * @param $type 0.未认证\1.18岁以下
    * @param $typeArr 需要认证房间类型
    */
    public function setJoinAllowType($type,$typeStr){
        if(empty($typeStr)){
            //删除
            return  Redis::HDEL($this->redis_join_allow, $type);
        }
        Redis::HSET($this->redis_join_allow, $type,$typeStr);
        return true;
    }


    /*
         * api接口
         * @param [$typeArr]   类型 默认 0  1 id合集
     *          * */
    public function getJoinAllowType($typeArr)
    {
        $res = [];
        foreach($typeArr as $k =>$v){
            $res[$v]['typeId'] = $v;
            if($v == 1){
                $res[$v]['typeName'] = '18岁可访问';
                $res[$v]['typeIds'] =  Redis::HGET($this->redis_join_allow, $v);
            }
            if($v == 0){
                $res[$v]['typeName'] = '未认证可访问';
                $res[$v]['typeIds'] =  Redis::HGET($this->redis_join_allow, $v);
            }
        }
        return $res;
    }
}